package com.yunqiao.service.api.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunqiao.common.consts.AppConsts;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.Query;
import com.yunqiao.service.api.MessageService;
import com.yunqiao.service.api.entity.MessageEntity;
import com.yunqiao.service.api.mapper.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("messageService")
public class MessageServiceImpl extends ServiceImpl<MessageMapper, MessageEntity> implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //各种查询条件的过滤,在此进行
        QueryWrapper<MessageEntity> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(MapUtil.getStr(params, "targetUserId"))) {
            queryWrapper.eq("target_user_id", MapUtil.getLong(params, "targetUserId"));
        }
        IPage<MessageEntity> page = this.page(new Query<MessageEntity>(params).getPage(),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public Boolean getNoReadMessage(Long userId) {
        int count = messageMapper.selectCount(new QueryWrapper<MessageEntity>()
            .eq("target_user_id", userId)
            .eq("is_read", AppConsts.MESSAGE_READ_1));
        if (count > 0) {
            return true;
        }
        return false;
    }

}
